package org.lalib.mediator;

import java.util.List;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.lalib.acquire.IMediator;
import org.lalib.entity.object.ECategory;
import org.lalib.entity.object.variable.EVariable;
import org.lalib.foundation.FDataAccess;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository(value = "mVariable")
@Scope(value = "prototype")
public class MVariable extends FDataAccess<EVariable> implements IMediator {

	private static final long serialVersionUID = 1L;

	@Override
	protected Class<EVariable> getClassType() {
		return EVariable.class;
	}

	@SuppressWarnings("unchecked")
	public final List<EVariable> getVariablesForGivenCategory(ECategory category) {
		return (List<EVariable>) getSession().createCriteria(EVariable.class).add(Restrictions.eq("category", category)).list();
	}

	public final boolean isVariableExist(String variableName) {
		return getSession().createCriteria(EVariable.class).add(Restrictions.eq("name", variableName)).uniqueResult() != null;
	}

	@SuppressWarnings("unchecked")
	public final List<String> getUniqueTabNamesForGivenVariables(List<ECategory> categories) {
		return getSession().createCriteria(getClassType()).setProjection(Projections.property("tabName"))
				.setProjection(Projections.distinct(Projections.property("tabName"))).add(Restrictions.in("category", categories)).list();
	}

	public final EVariable getVariableByName(String name) {
		return ((EVariable) getSession().createCriteria(getClassType()).add(Restrictions.eq("name", name)).uniqueResult());
	}

	@SuppressWarnings("unchecked")
	public final List<EVariable> getVariablesForTabname(String tabname) {
		return ((List<EVariable>) getSession().createCriteria(getClassType()).add(Restrictions.eq("tabName", tabname)).list());
	}
}